package cn.cdnsu.auth.service.impl;

import cn.cdnsu.auth.domain.Menu;
import cn.cdnsu.auth.dto.MenuDTO;
import cn.cdnsu.auth.mapper.MenuMapper;
import cn.cdnsu.auth.service.IMenuServiceI;
import cn.cdnsu.base.service.impl.IBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MenuServiceImplI extends IBaseServiceImpl<Menu> implements IMenuServiceI {

    @Autowired
    private MenuMapper mapper;


    @Override
    public void update(Menu menu) {
        Long id = menu.getParent().getId();
        if (Objects.isNull(id)){
            menu.setPath("/"+menu.getId());
        }else{
            Menu parent = mapper.selectById(id);
            menu.setPath(parent.getPath()+"/"+menu.getId());
        }
        mapper.update(menu);
    }

    @Override
    public void insert(Menu menu) {
        mapper.insert(menu);
        //处理path
        Long id = menu.getParent().getId();
        if (Objects.nonNull(id)){
            Menu parent = mapper.selectById(id);
            menu.setPath(parent.getPath()+"/"+menu.getId());
        }else{
            menu.setPath("/"+menu.getId());
        }
        mapper.update(menu);
    }


    @Override
    public List<Menu> menuTree() {
        return mapper.menuTree();
    }

    @Override
    public List<Long> selectByMenuId(Long id) {
        return mapper.selectByMenuId(id);
    }

    @Override
    public void insertByMenuId(MenuDTO menuDTO) {
        mapper.insertByMenuId(menuDTO);
    }

    @Override
    public List<Menu> queryMenuTree(Long id) {
        return mapper.queryMenuTree(id);
    }
}
